<?php

namespace app\api\controller\xyvenue;
use app\common\controller\Api;

use app\api\model\xyvenue\Coupon as CouponModel;
use app\api\model\xyvenue\user\Coupon as UserCouponModel;


use Think\Db;

/**
 * 优惠券接口
 */
class Coupon extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    

    /**
	 * 列表
	 */
	public function lists()
    {
    	$params = $this->request->post();
        $data = CouponModel::getLists($params);
        $this->success('订单列表', $data);
    }

	/**
     * 领取
     */
    public function receive() {
        $params = $this->request->post();
        $data = CouponModel::receive($params);
        $this->success('领取成功', $data);
    }
    
    
     /**
	 * 列表
	 */
	public function addCoupon()
    {
    // 获取请求数据
        $data =  $this->request->post();
        
        // 进行数据验证
        $validate = new \think\Validate([
            'facevalue' => 'require',
            'buyprice'  => 'require',
            'remark'    => 'require',
            // 'status'   => 'require',
        ]);
        
        if (!$validate->check($data)) {
            // return json(['code' => 0, 'msg' => $validate->getError()]);
        }
        
        // 模型实例化
        $model = new CouponModel();
        
        $data['status'] =  'normal';
        
        // 数据保存
        if ($model->allowField(true)->save($data)) {
            
            return $this->success('保存成功');
            
        } else {
            return $this->success('保存失败');
        }
        
    }
    
    
      /**
	 * 列表
	 */
	public function sendCoupon()
    {
    // 获取请求数据
        $params =  $this->request->post();
        
        // 进行数据验证
        $validate = new \think\Validate([
            'mobile' => 'require',
            'coupon_id' => 'require',
            'store_id' => 'require',
            'num'=>'require',
            // 'status'   => 'require',
        ]);
        
        if (!$validate->check($params)) {
            // return json(['code' => 0, 'msg' => $validate->getError()]);
        }
        
        
        //  if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
        //     $params[$this->dataLimitField] = $this->auth->id;
        // }

        if($params['num'] < 1 || $params['num'] > 500){
            $this->error(__('赠送数量输入错误', ''));
        }

        $couponList = [];
        $coupon = CouponModel::get($params['coupon_id']);
        $endtime = 0;
        if($coupon['validitytype'] == 0){
            $endtime = $coupon['endusetime'];
        }

        if($coupon['validitytype'] == 1){
            $endtime = time() + $coupon['fixedterm'] * 3600 * 24;
        }
        
        $user = Db::name('user')->where('mobile',$params['mobile'])->find();
        
        if(!$user){
            
            $this->error('没有找到用户');
            
        }
        
      

        for($i = 1; $i<=$params['num'];$i++){
            $couponList[] = [
                'store_id'  => $coupon['store_id'],
                'user_id'   => $user['id'],
                'coupon_id' => $params['coupon_id'],
                'type'      => $coupon['type'],
                'name'      => $coupon['name'],
                'money'     => $coupon['money'],
                'atleast'   => $coupon['atleast'],
                'discount'  => $coupon['discount'],
                'gettype'      => 2,
                'endtime'      => $endtime
            ];
        }
        
      
        
        // 模型实例化
        $model = new UserCouponModel();
        
        
      
        
        // 数据保存
        if ($model->allowField(true)->saveAll($couponList)) {
            
            return $this->success('保存成功');
            
        } else {
            return $this->success('保存失败');
        }
        
    }
    
    
     /**
	 * 列表
	 */
	public function CouponLists()
    {
    	$params = $this->request->post();
        $data = CouponModel::getStoreLists($params);
        $this->success('订单列表', $data);
    }
    
    
     /**
	 * 列表
	 */
	public function UserCouponLists()
    {
    	$params = $this->request->post();
        $data = UserCouponModel::getStoreLists($params);
        $this->success('订单列表', $data);
    }
    
    
}